<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <meta name="viewport" content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0" />
    <title>双良</title>
    <link rel="stylesheet" type="text/css" href="../../css/api.css" />
    <link rel="stylesheet" type="text/css" href="../../css/common.css" />
    <link rel="stylesheet" type="text/css" href="../../css/aui.css" />
    <link rel="stylesheet" type="text/css" href="../../css/aui-pull-refresh.css" />
    <style>
        html,
        body {
            width: 100%;
            min-height: 100%;
            background: #ffffff;
        }

        .aui-active .aui-bar-tab-label {
            color: #f5a51c
        }

        .aui-tab-item.aui-active {
            color: #f23030;
            border-bottom: none;
            position: relative;
        }

        .aui-tab-item.aui-active:after {
            position: absolute;
            content: '';
            width: 2.5rem;
            height: 2px;
            background-color: #f23030;
            border-radius: 2px;
            bottom: 0;
            left: 50%;
            margin-left: -1.25rem;
        }

        [v-cloak] {
            display: none;
        }

        #header_top {
            background-size: 0px;
        }

        #tab .aui-tab-item {
            /*min-height: 45px;*/
            /*font-size: 0.9rem;*/
        }

        #header_top .aui-pull-left {
            font-size: 0.6rem;
        }

        #header_top .aui-pull-left img {
            width: 0.7rem;
            margin-left: 0.2rem;
        }

        #header_top .aui-pull-right {
            margin-top: 13px;
        }

        #header_top .aui-pull-right .aui-icon-search {
            line-height: 1rem;
            font-size: 1rem;
        }

        #header_top .aui-pull-right img {
            width: 1rem;
            margin-left: 0.5rem;
            display: inline-block;
            vertical-align: middle;
        }

        .aui-bar-nav.aui-bar-light .aui-iconfont {
            color: #333;
        }
    </style>
</head>

<body>
    <div id="app">

        <header id="header_top" class="aui-bar aui-bar-nav aui-bar-light">
            <a class="aui-pull-left" onclick="openLocation()">
                <i class="aui-iconfont aui-icon-location"></i><span style="width: 5rem;overflow: hidden;height: 2.25rem;text-align: left;">定位中...</span>
            </a>
            <div class="aui-title">
                <div class="aui-tab" id="tab" style="width:100%">
                    <div class="aui-tab-item aui-active">买赠区 </div>
                    <div class="aui-tab-item">
                        <div></div>分销区 </div>
                </div>
            </div>
            <a class="aui-pull-right">
                <span class="aui-iconfont aui-icon-search" onclick="open_shopSearch_win()"></span>
                <img src="../../image/scan.png" alt="" onclick="ewm()">
            </a>
        </header>




    </div>
</body>
<script type="text/javascript" src="../../script/api.js"></script>
<script type="text/javascript" src="../../script/zepto.js"></script>
<script type="text/javascript" src="../../script/vue.min.js"></script>
<script type="text/javascript" src="../../script/utils.js"></script>
<script type="text/javascript" src="../../script/common.js"></script>
<script type="text/javascript" src="../../script/aui-tab.js"></script>
<script type="text/javascript" src="../../script/aui-scroll.js"></script>
<script type="text/javascript" src="../../script/aui-pull-refresh.js"></script>
<script type="text/javascript">
    var FNScanner = null
    var bMap = null
    var nowIndex = 1
    var gps = false

    var header_top = null
    var header_topPos = null

    function ewm() {

        FNScanner.open({
            autorotation: true,
        }, function(ret, err) {
            if (ret.eventType == 'success') {
                var obj = JSON.parse(ret.content);
                // alert(JSON.stringify(obj))
                if (obj.type == 1) {
                    api.openFrame({
                        name: 'pay_order2',
                        url: '../three_frame/xx_pay_order.html',
                        bounces: false,
                        pageParam: {
                            super: obj.super
                        }
                    });
                } else if (obj.type == 2) {
                    // alert(JSON.stringify(obj))
                    var jm_string = Base64.decode(obj.store_id)
                    var store_id = jm_string.slice(3, -5);
                    api.openWin({
                        name: 'shop_inner',
                        url: '../two_frame/two_frame_header.html',
                        pageParam: {
                            type: 'shop_inner',
                            name: obj.name,
                            id: store_id
                        }
                    })
                }

            } else {
                // alert(JSON.stringify(err));
                return
            }
        });
    }

    function openLocation() {
        api.openWin({
            name: 'location',
            url: './location.html',
            pageParam: {
                gps: gps
            }
        })
    }

    function getCity() {
        bMap.getLocation({
            autoStop: true,
            filter: 1
        }, function(ret, err) {
            if (ret.status) {
                bMap.getNameFromCoords({
                    lon: ret.lon,
                    lat: ret.lat
                }, function(ret, err) {
                    if (ret.status) {
                        if (!$api.getStorage('city')) {
                            gps = ret
                            $api.setStorage('province', ret.province)
                            $api.setStorage('city', ret.city)
                            $api.setStorage('longitude', ret.lon)
                            $api.setStorage('latitude', ret.lat)
                            $api.setStorage('address', ret.address)
                            $('#header_top .aui-pull-left span').text(ret.address)
                            province = ret.province
                            city = ret.city
                            longitude = ret.lon
                            latitude = ret.lat
                            address = ret.address
                            api.closeFrame({
                                name: 'product_default'
                            })
                            openDetail(nowIndex)
                        } else {
                            gps = ret
                            gps.address = $api.getStorage('address')
                            gps.lon = $api.getStorage('lon')
                            gps.lat = $api.getStorage('lat')
                            $('#header_top .aui-pull-left span').text($api.getStorage('address'))
                            api.openFrame({
                                name: 'product1',
                                url: './product1.html',
                                rect: {
                                    x: 0,
                                    y: header_topPos.h,
                                    w: 'auto',
                                    h: api.frameHeight - header_topPos.h
                                },
                                pageParam: {
                                    province: $api.getStorage('province'),
                                    city: $api.getStorage('city'),
                                    longitude: $api.getStorage('longitude'),
                                    latitude: $api.getStorage('latitude'),
                                },
                                bounces: false,
                                delay: 200
                            })
                        }
                    }
                });
            } else {
                api.toast({
                    msg: '获取定位信息失败',
                    duration: 2000,
                    location: 'middle'
                });
                gps = false
                $('#header_top .aui-pull-left span').text('定位失败')
            }
        });
    }
    // 假的登录
    function login() {
        api.ajax({
            url: window.Url.Freeuser_loginin,
            method: 'post',
            data: {
                values: {
                    mobile: Base64.encode('UNO' + '17600133590' + 'HACHA'),
                    type: 1,
                    password: '123456',
                    push_id: api.deviceId
                }
            }
        }, function(ret, err) {
            if (ret) {
                console.log('登录-----' + JSON.stringify(ret))

            } else {
                api.alert({
                    msg: JSON.stringify(err)
                });
            }
        })
    }




    apiready = function() {
        FNScanner = api.require('FNScanner');
        bMap = api.require('bMap');
        getCity()
        header_top = $api.byId('header_top');
        header_topPos = $api.offset(header_top);
        $api.fixStatusBar(header_top);

        api.openFrame({
            name: 'product_default',
            pageParam: {
                name: 'product_default'
            },
            rect: {
                x: 0,
                y: header_topPos.h,
                w: 'auto',
                h: api.frameHeight - header_topPos.h
            },
            url: './' + 'product_default.html',
            delay: 200,
            bounces: false,
            scrollEnabled: true,
            opaque: false
        });
    };

    function openDetail(index) {

        var header_top = $api.byId('header_top');
        var header_topPos = $api.offset(header_top);
        if ($api.getStorage('province') && $api.getStorage('city') && $api.getStorage('longitude') && $api.getStorage('latitude')) {
            api.closeFrame({
                name: 'product_default'
            });
            api.openFrame({
                name: 'product' + index,
                pageParam: {
                    province: $api.getStorage('province'),
                    city: $api.getStorage('city'),
                    longitude: $api.getStorage('longitude'),
                    latitude: $api.getStorage('latitude'),
                },
                rect: {
                    x: 0,
                    y: header_topPos.h,
                    w: 'auto',
                    h: api.frameHeight - header_topPos.h
                },
                url: './' + 'product' + index + '.html',
                delay: 200,
                bounces: false,
                scrollEnabled: true,
                opaque: false
            });
        } else {
            api.openFrame({
                name: 'product_default',
                pageParam: {
                    name: 'product_default'
                },
                rect: {
                    x: 0,
                    y: header_topPos.h,
                    w: 'auto',
                    h: api.frameHeight - header_topPos.h
                },
                url: './' + 'product_default.html',
                delay: 200,
                bounces: false,
                scrollEnabled: true,
                opaque: false
            });
        }


    }
    var tab = new auiTab({
        element: document.getElementById("tab"),
        repeatClick: false
    }, function(ret) {
        nowIndex = ret.index
        openDetail(nowIndex)
    });

    // 打开搜索窗口
    function open_shopSearch_win() {
        api.openWin({
            name: 'search_shop',
            url: '../two_frame/search_shop.html',
            pageParam: {
                name: 'test',
                searchType: 1,
                store_id: ''
            }
        });

    }
</script>

</html>
